<?php

namespace App\Service\WorkOrder\Web;

use App\Models\WorkOrder\WorkflowV2TypeModel;
use App\Service\BaseService;
use Kuafu\File\FileUtil;

class WorkflowV2TypeService extends BaseService
{
    /**
     * 获得列表
     * @param int $pageNum
     * @param int $pageSize
     * @param bool $isPaginate
     * @return array
     */
    public static function list(int $pageNum = 1, int $pageSize = 10, bool $isPaginate = false): array
    {
        if ($isPaginate) {
            // 分页查询
            $workOrderTypeModel = WorkflowV2TypeModel::query()
                ->orderBy('sort')
                ->paginate($pageSize, ["*"], "pageNum", $pageNum);

            $list = collect($workOrderTypeModel->items());
        } else {
            $list = WorkflowV2TypeModel::query()
                ->orderBy('status')
                ->orderBy('sort')
                ->get();
        }

        $data = [];
        $list->map(function ($item) use (&$data) {
            $data[] = [
                'name'        => $item->name,
                'description' => $item->description,
                'sort'        => $item->sort,
                'prefix'      => $item->prefix,
                'status'      => $item->status,
                'statusText'  => WorkflowV2TypeModel::STATUS_MAPPING[$item->status] ?? '-',
                'icon'        => FileUtil::domain() . '/' . $item->icon,
                'id'          => $item->id,
            ];
        });

        return $data;
    }

    /**
     * 添加&编辑
     * @param int $id
     * @param string $name
     * @param string $prefix
     * @param string $description
     * @param string $icon
     * @param int $sort
     * @return void
     */
    public static function save(int $id, string $name, string $prefix, string $description, string $icon, int $sort)
    {
        if ($id) {
            $workOrderModel       = WorkflowV2TypeModel::query()->find($id);
            $workOrderModel->name = $name;
        } else {
            // 查询该分类是否存在
            $existType = WorkflowV2TypeModel::query()->where('name', $name)->exists();

            if ($existType) {
                self::throwErr('该分类已存在', 400);
            }

            $workOrderModel = WorkflowV2TypeModel::query()
                ->withoutGlobalScope('isDel')
                ->updateOrCreate([
                    'name' => $name
                ]);
            $workOrderModel->status = WorkflowV2TypeModel::STATUS_OFF; // 默认是禁用
        }

        $workOrderModel->description = $description;
        $workOrderModel->prefix      = $prefix;
        $workOrderModel->type        = WorkflowV2TypeModel::TYPE_WORK_ORDER;
        $workOrderModel->sort        = $sort;
        $workOrderModel->icon        = ltrim(parse_url($icon, PHP_URL_PATH), '/');
        $workOrderModel->isDeleted   = 0;
        $workOrderModel->save();
    }

    /**
     * 删除审批分类
     * @param $id
     * @return void
     */
    public static function delete($id)
    {
        // 查询该分类是否被使用中
        // TODO 是否存在审批流中

        WorkflowV2TypeModel::query()
            ->where('id', $id)
            ->update([
                'isDeleted' => 1
            ]);
    }

    /**
     * 状态修改
     * @param int $id
     * @param int $status
     * @return void
     */
    public static function updateStatus(int $id, int $status)
    {
        // 查询该分类是否存在
        $existType = WorkflowV2TypeModel::query()->where('id', $id)->exists();

        if (!$existType) {
            self::throwErr('该分类不存在', 400);
        }

        WorkflowV2TypeModel::query()
            ->where('id', $id)
            ->update([
                'status' => $status
            ]);
    }
}